package mango.web.server.admin;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import mango.web.client.C;
import mango.web.client.admin.ContactVo;
import mango.web.client.admin.EmailVo;
import mango.web.client.admin.MemberVo;
import mango.web.client.admin.OrchardVo;
import mango.web.server.Log;
import mango.web.server.S;
import mango.web.server.ViewS;

public class EmailThread extends Thread {
	public ViewS parent;

	public String STMPserver;
	public String email;
	public String account;
	public String pass;
	public Boolean starttls = false;
	public Properties properties;
	public Integer port = 587;

	public String orchardEmailSubject;
	public String orchardEmailContent;

	public volatile boolean done = false;

	public void setProperties() {
		this.setName(this.email);
		properties = System.getProperties();
		properties.setProperty("mail.smtp.host", STMPserver);
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.smtp.connectiontimeout", "20000");
		properties.setProperty("mail.smtp.timeout", "20000");
		properties.setProperty("mail.smtp.port", this.port.toString());
		if (starttls == true) properties.setProperty("mail.smtp.starttls.enable", "true");
	}

	public void run() {
		/*
		while (!done) {
			try {
				Thread.sleep(10);
				long start = System.currentTimeMillis();
				EmailVo emailVo = null;
				synchronized (S._emailVoList) {
					for (int i = 0; i < S._emailVoList.size(); i++) {
						EmailVo _emailVo = S._emailVoList.get(i);
						if (!"sending".equals(_emailVo.statusEmail) && !"sent".equals(_emailVo.statusEmail) && !"skip".equals(_emailVo.statusEmail) && _emailVo.attempt <= 3) {
							emailVo = _emailVo;
							emailVo.statusEmail = "sending";
							break;
						}
					}
				}
				if (emailVo == null) {
					this.done = true;
					return;
				}

				try {
					if ("".equals(emailVo.email)) {
						emailVo.statusEmail = "skip";
						continue;
					}
					System.out.println(emailVo.email);
					String to = emailVo.email;
					Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(account, pass);
						}
					});
					MimeMessage message = new MimeMessage(session);
					message.setFrom(new InternetAddress(email));
					message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

					MemberVo member = S.memberMap.get(emailVo.memberId);
					ContactVo contact = S.contactMap.get(emailVo.contactId);
					OrchardVo orchard = S.orchardMap.get(emailVo.orchardId);

					message.setSubject(C.templateToString(this.orchardEmailSubject, member, contact, orchard));
					message.setContent(C.templateToString(this.orchardEmailContent, member, contact, orchard), "text/html");
					Transport.send(message);
					emailVo.statusEmail = "sent";
				} catch (Exception ex) {
					ex.printStackTrace();
					emailVo.attempt++;
					emailVo.statusEmail = "resend";
				}
				long end = System.currentTimeMillis();
				emailVo.time += (int) (end - start) / 1000;
			} catch (Exception ex) {
				ex.printStackTrace();
				Log log = Log.get(this.parent.getFilePath("log.log"));
				log.appendFile(ex.getMessage() + "\n" + ex.getStackTrace());
			}
		}
		*/
	}
}
